/**
 * 随机颜色
 * @param {String} ()
 * @returns String
 */
function color() {
    for(i=0;i<6;i++){
        var num=Math.floor(Math.random()*arr.length);
        str +=arr[num];
    }
    return str;
}


/**
 * 补零
 * @param {String} (时间)
 * @returns String
 */
function addzero(time) {
    return String(time).length===1 ? "0"+time:time;  //根据time长度值判断
    // return time < 10 ? "0" + time : time;  根据time值判断
}
/**
 * 获取当前日期年月日
 * @param {String} 
 * @returns String
 */
function format(str) {
    var d = new Date()
    str = str.replaceAll('Y', d.getFullYear())
    str = str.replaceAll('m', addzero(d.getMonth() + 1))
    str = str.replaceAll('d', addzero(d.getDate()))
    str = str.replaceAll('H', addzero(d.getHours()))
    str = str.replaceAll('i', addzero(d.getMinutes()))
    str = str.replaceAll('s', addzero(d.getSeconds()))
    return str
}

/**
 * 获取输入未来时间距今时间
 * @param {String} 
 * @returns String
 */
function weilai_time(time) {
    var date=new Date();
    var qixi=new Date(time);
    var x=date.getTime();
    var y=qixi.getTime();
    var tian=parseInt((y-x)/1000/60/60/24%365);
    var shi=parseInt((y-x)/1000/60/60%24);
    var fen=parseInt((y-x)/1000/60%60);
    var miao=parseInt((y-x)/1000%60);
    if (tian<=0&&shi<=0&&fen<=0&&miao<=0) {
        document.querySelector('#demo1').innerHTML="输入时间已过~";
    }else{
        document.querySelector('#demo1').innerHTML="输入时间距离现在还有"+tian+"天"+addzero(shi)+":"+addzero(fen)+":"+addzero(miao);
    }
}


/**
 * 获取输入时间距今时间
 * @param {String} 
 * @returns String
 */
function dateFormat(time) {
    let nowTime = new Date()
    let nowTimes = nowTime.getTime()
    let oldTime = new Date(time)
    let oldTimes = oldTime.getTime()
    let num_time = (nowTimes - oldTimes) / 1000    //当前时间距离输入时间相差的秒
    
    if (num_time < 10) return '刚刚'
    if (num_time < 60) return `${num_time}秒前`
    if (num_time < 60 * 60) return `${Math.floor(num_time / 60)}分钟前`
    if (num_time < 60 * 60 * 60) return `${Math.floor(num_time / 60 / 60)}小时前`
    if (num_time < 60 * 60 * 60 * 24) return `${Math.floor(num_time / 60 / 60 / 24)}天前`
    if (num_time < 60 * 60 * 60 * 24 * 30) return `${Math.floor(num_time / 60 / 60 / 24 / 30)}月前`
    if (num_time < 60 * 60 * 60 * 24 * 30 * 12) return `${Math.floor(num_time / 60 / 60 / 24 / 30 / 12)}年前`
}

 /**
 * 判断浏览器类型
 * @param {} 
 * @returns {str}
*/
function browser(){

    if(navigator.userAgent.indexOf('Firebox')!== -1){
     return '火狐浏览器'
    } else if(navigator.userAgent.indexOf('Chrome')!== -1){
    return '谷歌浏览器'
    } else if(navigator.userAgent.indexOf('Safari')!== -1){
        return '苹果浏览器'
    } else if(navigator.userAgent.indexOf('Opera')!== -1){
        return 'Opera浏览器'
    } else if(navigator.userAgent.indexOf('Trident')!== -1){
        return 'IE浏览器'
    }
      
} 

/**
 * 判断是PC还是移动端
 * @param {} 
 * @returns {str}
*/
function PcandYd() {
    data = navigator.userAgent
    if ((data.indexOf('Mac')||data.indexOf('Windows')||data.indexOf('Linux'))||data.indexOf('win') !== -1){
        return '电脑端'
    } 
    if ((data.indexOf('iPhone')||data.indexOf('iPad')||data.indexOf('Android')||data.indexOf('Mobile')) !== -1){
        return '移动端'
    } 
}
    


/**
 * 判断是否是微信内置浏览器
 * @param {} 
 * @returns {str}
*/
function weixin(){
    var ua = navigator.userAgent.toLowerCase();
 if(ua.match(/MicroMessenger/i)=="micromessenger") {
         return true;
     } else {
        return false;
     }
 }

  /**
  * 判断标签样式
  * @param object string 举例('divObj',)
  * @return string
  */

   function getStyle(tagObj,cssAttr){
    if(window.getComputedStyle){
        return getComputedStyle(tagObj)[cssAttr] //对象的键taObj是变量
    } else{
        return tagObj.currentStyle[cssAttr]
    }

}

/**
 * 获取地址栏
 * @param  {string} 请求参数：键
 * @return {string/boolean}
 */

function getParams(key){
    let temp =decodeURI(window.location.search.substring(1));
    let param = temp.split('&')
    let obj= {}
    // for(let i=0;i<param.length;i++){
    //     let item = param[i].split('=');
    //     if (item[0]==key) {
    //         return item[1]
    //     }
    // }
    // return false
    param.forEach((item,i)=>{
        let temp =item.split('=')
        let key = temp[0]
        let val = temp[1]
        obj[key]=val
    })
    if (key) {
        return obj[key]
    } 
    return obj
}
